Ext.require('AM.component.menu.MenuFactory')

Ext.define('AM.service.Support', {
    singleton:true,


    contextmenu:function (o) {
        o.el.dom.addEventListener('contextmenu', function (e) {
            e.stopPropagation();
            e.preventDefault();

            ppt.cache.rightClickDom = this;
            var menu = null;
            switch (o.xtype) {
                case 'xbxText':
                    menu = AM.component.menu.MenuFactory.textMenu();
                    break;
                case 'xbxImage':
                    menu = AM.component.menu.MenuFactory.imageMenu();
                    break;
                case 'xbxAudio':
                    menu = AM.component.menu.MenuFactory.audioMenu();
                    break;
                case 'xbxVideo':
                    menu = AM.component.menu.MenuFactory.videoMenu();
                    break;
                case 'xbxShape':
                    menu = AM.component.menu.MenuFactory.shapeMenu();
                    break;
                case 'xbxPage':
                    menu = AM.component.menu.MenuFactory.pageMenu();
                    break;
                case 'xbxTable':
                    menu = AM.component.menu.MenuFactory.tableMenu();
                    break;
            }
            if (menu != null)
                menu.showAt([e.clientX, e.clientY])

        })
    },


    unResize:function (o) {
        o.el.setStyle('resize', 'none');
    },


    resize:function (o) {
        o.addListener('ren')
        o.el.setStyle('resize', 'both');
        o.el.setStyle('overflow', 'hidden');
    },


    drag:function (o) {
        o.initDraggable();
        o.dd.onEnd = function () {
            AM.service.Thumbnails.refresh(ppt.index);
        }
    },


    // 图片拖拽的支持
    dropImg:function (o) {
        o.el.dom.addEventListener('drop', function (e) {
            e.stopPropagation();
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            if (!file.type.match('image'))return;
            var reader = new FileReader();
            reader.onload = function (re) {

                var img = Ext.create("AM.component.Image", {src:re.target.result});
                o.add(img);
                AM.service.Thumbnails.refresh(ppt.index);
            }
            reader.readAsDataURL(file)
        })
    },

    dropAudio:function (o) {
        o.el.dom.addEventListener('drop', function (e) {
            e.stopPropagation();
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            if (!file.type.match('audio/mp3') || !file.type.match('audio/wma' || !file.type.match('audio/wav')))return;
            var reader = new FileReader();
            reader.onload = function (re) {
                var audio = Ext.create("AM.component.Audio", {src:re.target.result});
                o.add(audio)
                AM.service.Thumbnails.refresh(ppt.index);
            }
            reader.readAsDataURL(file)
        })
    },

    dropVideo:function (o) {
        o.el.dom.addEventListener('drop', function (e) {
            e.stopPropagation();
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            if (!file.type.match('audio/ogg'))return;
            var reader = new FileReader();
            reader.onload = function (re) {
                var video = Ext.create("AM.component.Video", {src:re.target.result});
                o.add(video);
                AM.service.Thumbnails.refresh(ppt.index);
            }
            reader.readAsDataURL(file)
        })
    },


    dropText:function (o) {
        o.el.dom.addEventListener('drop', function (e) {
            e.stopPropagation();
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            var reader = new FileReader();
            reader.onload = function (re) {
                var cmp = Ext.create("AM.component.Text", {html:re.target.result});
                o.add(cmp);
                AM.service.Thumbnails.refresh(ppt.index);
            }
            reader.readAsText(file)
        })
    },


    drop:function (o) {
        o.el.dom.addEventListener('drop', function (e) {
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            if (file != null) {
                var reader = new FileReader();
                var cmpName = '';
                switch (file.type) {
                    case 'audio/ogg':
                        cmpName = 'Video';
                        break;

                    case 'audio/mp3':
                    case 'audio/wav':
                    case 'audio/wma':
                        cmpName = 'Audio';
                        break;

                    case 'image/jpeg':
                        cmpName = 'Image';
                        break;

                    case 'text/plain':
                        reader.onload = function (re) {
                            var cmp = Ext.create("AM.component.Text", {html:re.target.result, file:file});
                            o.add(cmp);
                            AM.service.Thumbnails.refresh(ppt.index);
                        }
                        reader.readAsText(file);
                        return;

                    default:
                        return;
                }
                reader.onload = function (re) {
                    var cmp = Ext.create("AM.component." + cmpName, {file:file});
                    o.add(cmp);
                    AM.service.Thumbnails.refresh(ppt.index);
                }
                reader.readAsDataURL(file)
            }

        })
    },

    dropRemoteImg:function (o) {
        o.el.dom.addEventListener('drop', function (e) {
            var imgName = e.dataTransfer.getData('imgName');
            AM.service.Insert.insertImgByName(imgName);
        })

        o.el.dom.addEventListener('dragover', function (e) {
            e.preventDefault();
        })
    },
    animShow: function(o) {
        o.animate({
            from:{
                width:0,
                height:0
            },
            to:{
                width:o.getWidth(),
                height:o.getHeight()
            }
        })
    },

    cacheCmp:function (o) {
        o.el.on('click', function (e) {
            if (o.xtype == 'xbxText') {
                e.stopEvent();
            }
            ppt.cache.currentCmp = o;

            if (ppt.view.property.collapsed)
                return;

            var store = Ext.create('Ext.data.Store', {
                fields:['key', 'value'],
                data:{'items':[
                    { key:'xtype', value:o.xtype },
                    { key:'背景色', value:o.el.dom.style.backgroundColor},
                    { key:'切换声音', value:o.cutOverAudioName },
                    { key:'动画', value:o.animName },
                    { key:'页码', value:ppt.index },
                    { key:'内容', value:o.el.dom.textContent }

                ]},
                proxy:{
                    type:'memory',
                    reader:{
                        type:'json',
                        root:'items'
                    }
                }
            });

            var g = Ext.create('Ext.grid.Panel', {
                store:store,
                columns:[
                    { header:'属性', dataIndex:'key' },
                    { header:'值', dataIndex:'value', flex:1 }
                ]
            });
            if (ppt.view.property.items.items.length != 0) {
                ppt.view.property.removeAll();
            }
            ppt.view.property.add(g)
        })
    }

});